import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import 'package:study_flutter/yzhg/dartTools/YEventBusUtil.dart';
import 'package:study_flutter/yzhg/project/shop/content/Content.dart';
import 'package:study_flutter/yzhg/project/shop/event/YEventBusBean.dart';

import 'shopcar/CardInfoBean.dart';
import 'shopcar/CartProvide.dart';
import 'shopcar/SuccessBack.dart';

class CarPage extends StatefulWidget {
  @override
  _CarPageState createState() => _CarPageState();
}

class _CarPageState extends State<CarPage> {
  StreamSubscription<YEventBusBean> _event;

  @override
  void initState() {
    super.initState();

    /*监听EventBus*/
    _event = YEventBusUtil.getInstance().on<YEventBusBean>().listen((data) {
      if (data.eventPosition == Content.EVENT_CART_CNAHGE) {
        /*重新载入布局*/
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("购物车"),
      ),
      body: FutureBuilder(
          future: _getCartInfo(context),
          builder: (context, snapshot) {
            List cartList = Provide.value<CartProvide>(context).cardInfoBean;
            if (snapshot.hasData) {
              return Stack(
                children: [
                  Container(
                    child: ListView.builder(
                        itemCount: cartList.length,
                        itemBuilder: (context, index) {
                          return _cartItemWidget(cartList[index]);
                        }),
                  ),
                  Positioned(
                      left: 0,
                      bottom: 0,
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.deepOrange
                        ),
                        width: ScreenUtil().setWidth(750),
                        height: ScreenUtil().setHeight(80),
                        alignment: Alignment.center,
                        child: Text("不想写了",style: TextStyle(
                          fontSize: ScreenUtil().setSp(28.0),
                          color: Colors.white
                        ),),
                      ))
                ],
              );
            } else {
              return Text("没有数据");
            }
          }),
    );
    //吉祥  何文静  飞杰   胜利  初中同学
  }

  /*获取购物车的数据*/
  Future<String> _getCartInfo(BuildContext context) async {
    await Provide.value<CartProvide>(context).getCartInfo();
    return "获取购物车数据成功";
  }

  /*子商品item布局 Widget*/
  Widget _cartItemWidget(CardInfoBean item) {
    return Container(
      decoration: BoxDecoration(color: Colors.white),
      width: ScreenUtil().setWidth(750),
      margin: EdgeInsets.only(top: 2.0),
      padding: EdgeInsets.only(top: 12.0, bottom: 12.0),
      child: Row(
        children: [
          _cartCheckBt(item),
          _cartItemImg(item),
          _cartItemTitle(item),
          _cartItemPrice(item)
        ],
      ),
    );
  }

  /*多选按钮*/
  Widget _cartCheckBt(CardInfoBean item) {
    return Container(
      child: Checkbox(
          value: false, activeColor: Colors.pink, onChanged: (bool val) {}),
    );
  }

  /*商品图片*/
  Widget _cartItemImg(CardInfoBean item) {
    return Container(
      width: ScreenUtil().setWidth(150),
      padding: EdgeInsets.all(3.0),
      decoration:
          BoxDecoration(border: Border.all(width: 1, color: Colors.black12)),
      child: Image.network(item.images),
    );
  }

  /*商品标题*/
  Widget _cartItemTitle(CardInfoBean item) {
    return Container(
      width: ScreenUtil().setWidth(300),
      padding: EdgeInsets.all(10),
      alignment: Alignment.topLeft,
      child: Column(
        children: [Text(item.goodsName), _cartItemNumChange(item)],
      ),
    );
  }

  /*商品的加减操作*/
  Widget _cartItemNumChange(CardInfoBean item) {
    return Container(
      margin: EdgeInsets.only(top: 8.0),
      width: ScreenUtil().setWidth(184),
      height: ScreenUtil().setHeight(40),
      //加上边框
      decoration:
          BoxDecoration(border: Border.all(width: 1.0, color: Colors.black12)),
      child: Row(
        children: <Widget>[
          _subtractWidget(item),
          _numWidget(item),
          _addWidget(item)
        ],
      ),
    );
  }

  /*减号操作*/
  Widget _subtractWidget(CardInfoBean item) {
    return Container(
      alignment: Alignment.center,
      width: ScreenUtil().setWidth(60),
      height: ScreenUtil().setHeight(40),
      decoration: BoxDecoration(
          border: Border(
              left: BorderSide(width: 1.0, color: Colors.black12),
              right: BorderSide(width: 1.0, color: Colors.black12))),
      child: InkWell(
        onTap: () {
          Provide.value<CartProvide>(context).subtractAdd(item.goodsId, false);
        },
        child: Text(
          "-",
          style: TextStyle(
              fontSize: ScreenUtil().setSp(28.0), color: Colors.black),
        ),
      ),
    );
  }

  /*数量*/
  Widget _numWidget(CardInfoBean item) {
    return Container(
      alignment: Alignment.center,
      width: ScreenUtil().setWidth(60),
      height: ScreenUtil().setHeight(40),
      color: Colors.white,
      child: Text("${item.count}"),
    );
  }

  /*加号操作*/
  Widget _addWidget(CardInfoBean item) {
    return Container(
      width: ScreenUtil().setWidth(60),
      height: ScreenUtil().setHeight(40),
      alignment: Alignment.center,
      decoration: BoxDecoration(
          border: Border(
              left: BorderSide(width: 1.0, color: Colors.black12),
              right: BorderSide(width: 1.0, color: Colors.black12))),
      child: InkWell(
        onTap: () {
          Provide.value<CartProvide>(context).subtractAdd(item.goodsId, true);
        },
        child: Text(
          "+",
          style: TextStyle(
              fontSize: ScreenUtil().setSp(28.0), color: Colors.black),
        ),
      ),
    );
  }

  /*商品的价格*/
  Widget _cartItemPrice(CardInfoBean item) {
    return Container(
      width: ScreenUtil().setWidth(150),
      alignment: Alignment.centerRight,
      child: Column(
        children: [
          Text("￥${item.price}"),
          InkWell(
            onTap: () {
              Provide.value<CartProvide>(context).deleteGoods(
                  item.goodsId,
                  SuccessBack(
                    onSuccess: (Object o) {
                      setState(() {});
                      print("================================删除成功");
                    },
                    onError: (Object e) {
                      // 请求失败的处理
                      print("================================删除失败" + e);
                    },
                  ));
            },
            child: Text(
              "删除",
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(28.0), color: Colors.pink),
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _event.cancel();
  }
}
